package com.tlgen.coopera.config;

import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;

/*
 *@title SecurityConfig
 *@description
 *@author ta151486
 *@version 1.0
 *@create 2023/12/15 16:23
 */
@Configuration
public class SecurityConfig {
    private KeyPair keyPair;

    @PostConstruct
    public void init() {
        // Generate RSA key pair
        KeyPairGenerator keyGen;
        try {
            keyGen = KeyPairGenerator.getInstance("RSA");
            keyGen.initialize(2048);
            keyPair = keyGen.genKeyPair();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Failed to generate RSA key pair", e);
        }
    }

    /**
     * 提供给前端获取RSA公钥
     * @return
     */
//    @Bean
//    public RouterFunction<ServerResponse> publicKeyEndpoint() {
//        return RouterFunctions.route()
//                .GET("/public-key", req -> {
//                    String publicKey = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());
////                    return ServerResponse.ok().bodyValue(publicKey);
//                    return ServerResponse.ok().body(publicKey);
//                })
//                .build();
//    }

    public KeyPair getKeyPair() {
        return keyPair;
    }

}
